/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.app.topic.comment.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.hibernate.validator.constraints.Length;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.mapper.JsonMapper;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.app.data.record.entity.AppDataRecord;
import com.jeesite.modules.app.data.record.service.AppDataRecordService;
import com.jeesite.modules.app.topic.comment.entity.AppTopicComment;
import com.jeesite.modules.app.topic.comment.service.AppTopicCommentService;
import com.jeesite.modules.conf.AppController;
import com.jeesite.modules.conf.MessageCode;

/**
 * 评论表Controller
 * @author zpp
 * @version 2020-02-03
 */
@Controller
@RequestMapping(value = "/app/topic/comment/")
public class AppTopicCommentController extends BaseController {

	@Autowired
	private AppTopicCommentService appTopicCommentService;
	@Autowired
	private AppDataRecordService appDataRecordService;
	@RequestMapping(value="add")
	public void add(AppTopicComment entity,HttpServletRequest request,HttpServletResponse response){
		String topicId = entity.getTopicId();
		if(StringUtils.isBlank(topicId)){
			AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED, "话题主题");
			return;
		}
		String commentator=entity.getCommentator();
		if(StringUtils.isBlank(commentator)){
			AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED, "评论人");
			return;
		}
		String commentContent=entity.getCommentContent();
		if(StringUtils.isBlank(commentContent)){
			AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED, "评论内容");
			return;
		}
		appTopicCommentService.save(entity);
		JSONObject json=JSONObject.parseObject(JsonMapper.toJson(entity));
		AppController.renderSuccessObject(response, json, MessageCode.SUCCESS_ACK);
	}
	@RequestMapping(value="addLikeNum")
	public void addLikeNum(AppTopicComment entity,HttpServletRequest request,HttpServletResponse response){
		String id=entity.getId();
		if(StringUtils.isBlank(id)){
			AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED, "标识");
			return;
		}
		String commentator=entity.getCommentator();
		if(StringUtils.isBlank(commentator)){
			AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED, "用户信息");
			return;
		}
		entity=appTopicCommentService.get(id);
		Integer likeNum = entity.getLikeNum();
		if(likeNum==null){
			likeNum=0;
		}
		entity.setLikeNum(likeNum+1);
		appTopicCommentService.update(entity);
		AppDataRecord adr=new AppDataRecord();		
		adr.setDataId(id);
		adr.setUserId(commentator);
		appDataRecordService.save(adr);		
		AppController.renderSuccessObject(response, "", MessageCode.SUCCESS_ACK);
	}
	
}